package com.lindroid.androiduilearning.expandablelistview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.lindroid.androiduilearning.R;

import java.util.List;

/**
 * @author Lin
 * @date 2018/4/17
 * @function ExpandableListView的适配器
 */

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> groupList, childList;

    public MyExpandableListAdapter(Context context, List<String> groupList, List<String> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupHolder;
        if (convertView == null) {
            groupHolder = new GroupViewHolder();
            convertView = View.inflate(context, R.layout.item_list_group, null);
            groupHolder.tvGroup = convertView.findViewById(R.id.tv_group);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupViewHolder) convertView.getTag();
        }
        groupHolder.tvGroup.setText((CharSequence) getGroup(groupPosition));
        groupHolder.tvGroup.setTextSize(18);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        ChildViewHolder childHolder;
        if (convertView == null) {
            childHolder = new ChildViewHolder();
            //设置子布局时，如果高度是固定的，出来的效果是wrap_content，要再加上一个minHeight属性才会有效果
            convertView = View.inflate(context, android.R.layout.simple_list_item_1, null);
            childHolder.tvChild = convertView.findViewById(android.R.id.text1);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildViewHolder) convertView.getTag();
        }
        childHolder.tvChild.setText((CharSequence) getChild(groupPosition, childPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        TextView tvGroup;
    }

    class ChildViewHolder {
        TextView tvChild;
    }

}
